﻿namespace PreGUI.Widgets
{
    using PreGUI.Interfaces;
    using UnityEngine;
    using UnityEngine.UI;
    using PreGUI.Helpers;

    /// <summary>
    /// PreGui ScrollWindow Control
    /// </summary>
    public class PGScrollWindow : IPGControl
    {
        

        public IPGControl Child;
        public float Height;
        public GameObject LayoutControls(PreGui gui, GameObject parent, string windowName)
        {
            var gameObject = new GameObject("PGScrollWindow");
            gameObject.transform.SetParent(parent.transform, false);

            {
                var layoutElement = gameObject.AddComponent<LayoutElement>();
                layoutElement.minHeight = Height;
                //var layout = gameObject.AddComponent<HorizontalLayoutGroup>();
                //layout.childForceExpandHeight = false;
                //layout.padding = new RectOffset(0, 0, 0, 0);
                //layout.spacing = 2f;
            }
            var contentImage = gameObject.AddComponent<Image>();
            contentImage.color = new Color(0f, 0f, 0f, 0.1f);

            var contentMask = gameObject.AddComponent<Mask>();
            contentMask.showMaskGraphic = true;

            var scrollRect = gameObject.AddComponent<ScrollRect>();

            var scrollBarObject = new GameObject("ScrollBar");
            scrollBarObject.transform.SetParent(gameObject.transform, false);
            var scrollBarObjectRect = scrollBarObject.AddComponent<RectTransform>();
            scrollBarObjectRect.anchorMin = new Vector2(1f, 0f);
            scrollBarObjectRect.anchorMax = new Vector2(1f, 1f);
            scrollBarObjectRect.pivot = new Vector2(1f, 0.5f);
            scrollBarObjectRect.sizeDelta = new Vector2(20f, 0f);

            var scrollBarImage = scrollBarObject.AddComponent<Image>();
            scrollBarImage.color = gui.TitleBarSettings.FontColor;

            var scrollBar = scrollBarObject.AddComponent<Scrollbar>();
            scrollBar.SetDirection(Scrollbar.Direction.BottomToTop, false);
            scrollBar.interactable = true;

            var slidingAreaObject = new GameObject("SlidingArea");
            slidingAreaObject.transform.SetParent(scrollBar.transform, false);
            var rectTransform = slidingAreaObject.AddComponent<RectTransform>();
            rectTransform.anchorMin = new Vector2(0f, 0f);
            rectTransform.anchorMax = new Vector2(1f, 1f);
            rectTransform.position = scrollBar.transform.position;
            rectTransform.offsetMin = new Vector2(0f, 0f);
            rectTransform.offsetMax = new Vector2(0f, 0f);


            var handleObject = new GameObject("Handle");
            handleObject.transform.SetParent(slidingAreaObject.transform, false);
            var handleTransform = handleObject.AddComponent<RectTransform>();
            handleTransform.anchorMin = new Vector2(0f, 0f);
            handleTransform.anchorMax = new Vector2(1f, 1f);
            handleTransform.position = slidingAreaObject.transform.position;
            handleTransform.offsetMin = new Vector2(0f, 0f);
            handleTransform.offsetMax = new Vector2(0f, 0f);

            var handleImage = handleObject.AddComponent<Image>();
            handleImage.color = gui.TitleBarSettings.BarColor;

            scrollRect.verticalScrollbar = scrollBar;
        
            scrollBar.handleRect = handleObject.GetComponent<RectTransform>();
            scrollRect.horizontal = false;
            scrollRect.vertical = true;

            var contentObject = new GameObject("Container");
            contentObject.transform.SetParent(gameObject.transform);
            var vertContent = contentObject.AddComponent<VerticalLayoutGroup>();
            vertContent.padding = new RectOffset(0, 20, 0, 0);
            var contentRect = contentObject.GetComponent<RectTransform>();
            contentRect.anchorMin = new Vector2(0f, 0f);
            contentRect.anchorMax = new Vector2(1f, 1f);
            contentRect.pivot = new Vector2(0.5f, 1f);
            contentRect.position = gameObject.transform.position;
            contentRect.offsetMin = new Vector2(0f, 0f);
            contentRect.offsetMax = new Vector2(0f, 0f);

            var subContentObject = new GameObject("Content");
            subContentObject.transform.SetParent(contentObject.transform);
            var subcontentRect = subContentObject.AddComponent<RectTransform>();
            subcontentRect.anchorMin = new Vector2(0f, 0f);
            subcontentRect.anchorMax = new Vector2(1f, 1f);
            subcontentRect.pivot = new Vector2(0.5f, 1f);
            subcontentRect.position = contentObject.transform.position;
            subcontentRect.offsetMin = new Vector2(0f, 0f);
            subcontentRect.offsetMax = new Vector2(0f, 0f);
            subContentObject.AddComponent<VerticalLayoutGroup>();

            scrollRect.content = contentRect;


            if (Child != null)
                Child.LayoutControls(gui, subContentObject, windowName);            

            return gameObject;
        }


        public IPGControl GenerateFromXmlNode(System.Xml.XmlNode node)
        {
            return new PGScrollWindow
            {
                Height = System.Convert.ToInt32(node.Attributes["Height"].Value),
                Child = node.HasChildNodes ? node.ChildNodes.Item(0).XmlProcessChildControl() : null
            };
        }
    }
}

